package demo

import (
	"github.com/flipped-aurora/gin-vue-admin/server/global"
	"github.com/flipped-aurora/gin-vue-admin/server/model/common/request"
	"github.com/flipped-aurora/gin-vue-admin/server/model/demo"
	demoReq "github.com/flipped-aurora/gin-vue-admin/server/model/demo/request"
)

type SysStudentService struct {
}

// CreateSysStudent 创建SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) CreateSysStudent(sysStudent *demo.SysStudent) (err error) {
	err = global.GVA_DB.Create(sysStudent).Error
	return err
}

// DeleteSysStudent 删除SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) DeleteSysStudent(sysStudent demo.SysStudent) (err error) {
	err = global.GVA_DB.Delete(&sysStudent).Error
	return err
}

// DeleteSysStudentByIds 批量删除SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) DeleteSysStudentByIds(ids request.IdsReq) (err error) {
	err = global.GVA_DB.Delete(&[]demo.SysStudent{}, "id in ?", ids.Ids).Error
	return err
}

// UpdateSysStudent 更新SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) UpdateSysStudent(sysStudent demo.SysStudent) (err error) {
	err = global.GVA_DB.Save(&sysStudent).Error
	return err
}

// GetSysStudent 根据id获取SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) GetSysStudent(id uint) (sysStudent demo.SysStudent, err error) {
	err = global.GVA_DB.Where("id = ?", id).First(&sysStudent).Error
	return
}

// GetSysStudentInfoList 分页获取SysStudent记录
// Author [piexlmax](https://github.com/piexlmax)
func (sysStudentService *SysStudentService) GetSysStudentInfoList(info demoReq.SysStudentSearch) (list []demo.SysStudent, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	// 创建db
	db := global.GVA_DB.Model(&demo.SysStudent{})
	var sysStudents []demo.SysStudent
	// 如果有条件搜索 下方会自动创建搜索语句
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Find(&sysStudents).Error
	return sysStudents, total, err
}
